package service;

import java.sql.Connection;
import java.sql.SQLException;

public abstract class AbstractConnection {
	private Connection conn=null;
	public Connection dataConnection() {
		String userName = "root";
		String password = "123456";
		String url = "jdbc:mysql://localhost/test?"
				+ "user=root&password=123456";
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = java.sql.DriverManager
					.getConnection(url, userName, password);
			return conn;
		} catch (SQLException e) {
			e.printStackTrace();

		}
		return null;
	}

	public void closeConnection(){
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	public Connection getCurrentConnection(){
		try {
			if(conn == null || conn.isClosed()) {
				conn = dataConnection();
				return conn;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return null;
	}
}
